package com.gz.steak.modules.system.controller;

import com.gz.steak.common.aspect.anotation.Auth;
import com.gz.steak.common.aspect.anotation.OperationLog;
import com.gz.steak.common.consts.Role;
import com.gz.steak.common.web.base.BaseController;
import com.gz.steak.modules.system.pojo.dataobject.SysDept;
import com.gz.steak.modules.system.pojo.vo.SysDeptVo;
import com.gz.steak.modules.system.service.SysDeptService;
import com.gz.steak.utils.convert.ConvertUtil;
import com.gz.steak.utils.jwt.TokenUtil;
import com.gz.steak.common.web.result.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author 刘牌
 * @Date 2020/3/20 0020 20:05
 * @Version 1.0
 */
@RestController
@RequestMapping("/dept")
public class SysDeptController extends BaseController {

    private final static Logger logger = LoggerFactory.getLogger(SysDeptController.class);

    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 添加部门
     * @param sysDept
     * @param request
     * @return
     */
    @Auth(Role.ADMIN)
    @PostMapping("/save")
    public Result save(@RequestBody SysDept sysDept , HttpServletRequest request){
        sysDept.setCreateBy(TokenUtil.getUser(request));
        sysDeptService.save(sysDept);
        return Result.success("success");
    }

    /**
     * 部门树
     * @return
     */
    @GetMapping("/get")
    public Result get(){
        List<SysDept> list =  sysDeptService.get();
        return Result.success(list);
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @Auth(Role.ADMIN)
    @DeleteMapping("/deleteById")
    public Result deleteById(@RequestParam Long id){
        sysDeptService.deleteById(id);
        return Result.success("success");
    }

    /**
     * 修改部门
     * @param sysDeptVo
     * @return
     */
    @OperationLog("修改部门")
    @Auth(Role.ADMIN)
    @PutMapping("/edit")
    public Result edit(@RequestBody SysDeptVo sysDeptVo){
        sysDeptService.edit(sysDeptVo);
        return Result.success("success");
    }
}
